<?php
class AppController extends Controller{
	var $view = 'Theme';
    var $theme = HOST;
	
    var $transaction;
    
	var $uses = array('Cm','CmsSection','User','Category');
	var $permissions = array(
//		'display' => array('admin','manag','emplo','users','firms','copyr')	
	);
	var $helpers = array('Session','Html','Text','Form','Time','Tree','Ajax','Javascript','Country','Gallery.Gallery',
						'Js' => array('Jquery'),'Cksource','Section','FileUpload.FileUpload','Google','Carts','Categories','Members.Members','Footpanel');
    var $components = array( 'Auth','Session','RequestHandler','Email','Cookie','CompCms',
    						'Calendar.Calendar','FileUpload.FileUpload','Search.Prg','Carts','Categories','Gallery.CGallery'
    						);
    
    function beforeRender(){
    	parent::beforeRender();
    	// for ckfinder
		$this->Session->write('HOST', HOST);
    	//read cms settings for page
		$this->CompCms->get();	
		$color = $this->Session->read('color');
		if(empty($color)){
			$this->Session->write('color','green');
		}
    }
	
    /**
     * (non-PHPdoc)
     * @see cake/libs/controller/Controller::beforeFilter()
     */
    function beforeFilter() {
		parent::beforeFilter();	
		
		$this->_setLanguage();       

		$this->Auth->authorize = 'controller';
        $this->Auth->loginAction = array('plugin'=>null,'controller' => 'users', 'action' => 'login');
        $this->Auth->loginRedirect = array('plugin'=>null,'controller' => 'pages', 'action' => 'display');
        $this->Auth->logoutRedirect = array('plugin'=>null,'controller' => 'users', 'action' => 'login');
		$this->Auth->loginError = __("Your username and/or password was not recognized. Use the form below in order to retrieve your login details.",true);
   	 	$this->Auth->authError = __("Proszę się zalogować by wejć do tej strony.",true);
        
//   	 	if(!$this->Auth->user()) 
        if($this->isAuthorized()) $this->Auth->allow($this->action);
        $this->transaction = ConnectionManager::getDataSource('default');
        
        
	}
    
	function kolor($color){
		$this->Session->write('color',$color);
		$this->redirect($this->referer());
	}
	
	function _setLanguage() {
		if(!isset($this->params['language'])){
			if ($this->Session->read('Config.language') != false && $this->Session->read('Config.language') != '') {     
				Configure::write('Config.language', $this->Session->read('Config.language'));				
			}else{
				// domyślny język
				Configure::write('Config.language', Configure::read('Config.default_lang'));
			}
		}else if(isset($this->params['language']) && $this->params['language'] != Configure::read('Config.language')){
			Configure::write('Config.language', $this->params['language']);
			$this->Session->write('Config.language',$this->params['language']);			
		}else{			
			// domyślny język
		    Configure::write('Config.language', Configure::read('Config.default_lang'));
		}
	}
	
    function isAuthorized(){ 
        if($this->Auth->user('group') == 'admin') return true; //Remove this line if you don't want admins to have access to everything by default 
        if(!empty($this->permissions[$this->action])){ 
           	if($this->permissions[$this->action] == '*') return true;            	
            if(in_array($this->Auth->user('group'), $this->permissions[$this->action])) return true;            
        }else{ 
		    // If there are no permissions, let everyone in 		    
		    $this->Auth->allow($this->action); 
		    return true; 
		}
		$this->Session->setFlash($this->Auth->authError);
		
		if($this->Session->check('Auth.User.id')){
			$this->redirect($this->referer());
		}else{
			$this->redirect($this->Auth->loginAction);
		}
        return false;         
    }
    
    function sendEmail(){}
    
    /**
    *
    * Przekszta�cenie struktury Tree do wy�wietlaneia w combobox lub na liscie
    * @param array $data // dane z zapytania $model->find('treahend');
    * @param string $model // nazwa modeluz kt�rego pobrano dane
    * @param bool $full // true: zwraca array dodaj�c jedynie wartosc depth; false: zwaraca list� id => name;
    */
    function treeForCombobox($data,$model,$full=false){
    	$parentCategories_ = $this->treeComboBox($data,$model);
    	if($full) return $parentCategories_;
    	$parentCategories = array();
    	foreach ($parentCategories_ as $key => $value) {
    		$parentCategories[$value[$model]['id']] = $value[$model]['name'];
    	}
    	return $parentCategories;
    }   

    /**
    *
    * Dodaje warto�� depth
    * @param $data
    * @param $model
    */
    private function treeComboBox($data,$model) {
    	$result = array();
    	foreach ($data as $i) {
    		$newData = $i;
    		$newData[$model]['depth'] = count($this->{$model}->getpath($i[$model]['id'])) - 1;
    		$newData[$model]['name'] = str_repeat(" . . . . . . . ", $newData[$model]['depth']).$newData[$model]['name'];
    
    		unset($newData['children']);
    		$result[] = $newData;
    		if(isset($i['children']) && is_array($i['children'])) {
    			$children = $this->treeComboBox($i['children'],$model);
    			foreach($children as $k) {
    				$result[] = $k;
    			}
    		}
    	}
    	return $result;
    }
    
}